package com.dyna.demo.feign.controller;

import com.dyna.core.exception.CustomException;
import com.dyna.demo.feign.service.OpenFeignService;
import com.dyna.domain.resp.R;
import com.dyna.foundation.domain.entity.SysUser;
import com.dyna.foundation.domain.resp.SysUserResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author zhoucaiwang
 */
@Api(value = "demo模块/测试远程调用")
@RequestMapping("/remote")
@RestController
@AllArgsConstructor
public class OpenFeignController {

    private final OpenFeignService openFeignService;

    @ApiOperation(value = "根据用户id查询用户信息")
    @GetMapping("/getUserInfoById")
    public R<SysUserResp> getUserInfoById(@RequestParam("userId") Long userId) {
        Optional.ofNullable(userId).orElseThrow(()-> new CustomException("用户id不能为空"));
        SysUserResp user = openFeignService.getUserInfoById(userId);
        return R.data(user);
    }

    @ApiOperation(value = "根据用户名称查询用户信息")
    @GetMapping("/getUserInfoByUsername")
    public R<SysUser> getUserInfoByUsername(@RequestParam("username") String username) {
        Optional.ofNullable(username).orElseThrow(()-> new CustomException("用户名不能为空"));
        SysUser user = openFeignService.getUserInfoByUsername(username);
        return R.data(user);
    }

    @ApiOperation(value = "测试远程调用异常")
    @GetMapping("/testException")
    public R<Boolean> testException(@RequestParam("userId") Long userId) {
        Optional.ofNullable(userId).orElseThrow(()-> new CustomException("用户id不能为空"));
        Boolean b = openFeignService.testException(userId);
        return R.isSuccess(b);
    }

}
